package xiangya.bysj.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import xiangya.bysj.entity.Patient;
import xiangya.bysj.entity.TakeCycle;
import xiangya.bysj.entity.TakeInfo;
import xiangya.bysj.mapper.interf.TakeInfoMapper;
import xiangya.bysj.service.interf.TakeInfoServiceInterface;

/**
 * 服药信息的业务实现类
 * 
 * @author Mr.zhang
 *
 */
@Service("takeInfoService") // 注册业务类
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class TakeInfoServiceImpl implements TakeInfoServiceInterface {

	@Resource(name = "takeInfoMapper")
	private TakeInfoMapper takeInfoMapper;

	@Resource(name = "patientService")
	private PatientServiceImpl patientService;

	@Autowired
	private HistoryTakeServiceImpl historyTakeService;

	/**
	 * 单个保存服药信息的业务方法
	 */
	@Override
	public void save(TakeInfo obj) throws Exception {
		int num = takeInfoMapper.save(obj);
		for (TakeCycle tc : obj.getTkCycle()) {
			tc.setTkId(obj.getTkId());
		}
		// 后添加 tkCycle
		if (obj.getTkCycle() != null) {
			saveCycleBatch(obj.getTkCycle());
			List<TakeInfo> list = new ArrayList<TakeInfo>();
			list.add(obj);
			historyTakeService.createHistBatch(list);
		}
	}

	/**
	 * 更新服药信息
	 */
	@Override
	public void update(TakeInfo obj) throws Exception {
		takeInfoMapper.update(obj);
		for (TakeCycle tc : obj.getTkCycle()) {
			tc.setTkId(obj.getTkId());
		}
		takeInfoMapper.deleteCycleByTkId(obj.getTkId());
		historyTakeService.deleteTodayByTk(obj.getTkId());
		if (obj.getTkCycle() != null) {
			takeInfoMapper.saveCycleBatch(obj.getTkCycle());
			List<TakeInfo> list = new ArrayList<TakeInfo>();
			list.add(obj);
			historyTakeService.createHistBatch(list);
		}
	}

	/**
	 * 删除服药信息
	 */
	public void delete(Integer objId) throws Exception {
		// 先删除tkCycle
		takeInfoMapper.deleteCycleByTkId(objId);
		takeInfoMapper.delete(objId);
	}

	/**
	 * 批量保存服药信息
	 */
	@Override
	public void saveBatch(List<TakeInfo> list) throws Exception {
		takeInfoMapper.saveBatch(list);
		historyTakeService.createHistBatch(list);
	}

	/**
	 * 根据服药者获取服药信息
	 */
	@Override
	public List<TakeInfo> getByPatientId(int patientId, String todayDate) throws Exception {
		return takeInfoMapper.getByPatientId(patientId, todayDate);
	}

	@Override
	public void saveCycleBatch(List<TakeCycle> list) throws Exception {
		takeInfoMapper.saveCycleBatch(list);
	}

	@Override
	public void saveCycle(TakeCycle takeCycle) throws Exception {
		takeInfoMapper.saveCycle(takeCycle);
	}

	public void deleteCycle(Integer cycleId) throws Exception {
		takeInfoMapper.deleteCycle(cycleId);
	}

	@Override
	public TakeInfo getById(int takeinfoId) throws Exception {
		return takeInfoMapper.selectById(takeinfoId);
	}

	@Override
	public List<TakeCycle> getCycleByTakeInfoId(int takeinfoId) throws Exception {
		return takeInfoMapper.selectCycleByTakeInfoId(takeinfoId);
	}

	@Override
	public List<TakeInfo> getByUser(int userId) throws Exception {
		List<Patient> list = patientService.findPatientsByUser(userId);
		List<TakeInfo> takeinfos = null;
		if (list != null) {
			int[] pIds = new int[list.size()];
			for (int i = 0; i < pIds.length; i++) {
				pIds[i] = list.get(i).getId();
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (pIds.length > 0) {
				takeinfos = takeInfoMapper.selectByPatientIds(pIds, sdf.format(new Date()));
			} else {
				takeinfos = new ArrayList<TakeInfo>();
			}
		} else {
			takeinfos = new ArrayList<TakeInfo>();
		}
		return takeinfos;
	}

	@Override
	public int getAmount(int tkId) throws Exception {
		return takeInfoMapper.selectAmount(tkId);
	}

	@Override
	public List<TakeInfo> getByPatients(int[] pIds) throws Exception {
		return takeInfoMapper.selectAllByPatientIds(pIds);
	}

	@Override
	public void delete(int objId) throws Exception {
		// 先删除tkCycle
		takeInfoMapper.deleteCycleByTkId(objId);
		takeInfoMapper.delete(objId);
		// 删除今日还没服用的
		historyTakeService.deleteTodayByTk(objId);
	}

	public void deleteCycle(int cycleId) throws Exception {

		takeInfoMapper.deleteCycle(cycleId);

	}
}
